package com.bigflexlib.ds.container.zet.impl
{
	
	
	import com.bigflexlib.core.comparison.result.ComparisonResult;
	import com.bigflexlib.ds.container.utils.ContainerUtil;
	import com.bigflexlib.ds.container.zet.IRWSet;
	import com.bigflexlib.utils.AutoUpdateFlag;


	/**
	 * 
	 * <p>
	 * A set that only holds ONE value at a time.
	 * It is useful in some situations... like running a set of a single result against a filter.
	 * </p>
	 * 
	 * <p>
	 * <b>A second call to add( ) with any value will do nothing</b>
	 * </p>
	 * 
	 * You cannot pass undefined as the value to be held.
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class RWSingleValueSet implements IRWSet
	{
		
		
		private var value:* = undefined;
		

		
		public function get isEmpty( ):Boolean
		{
			return value === undefined;
		}
		
		
		public function add( e:* ):void
		{
			if ( isEmpty )
				value = e;		
		}
		
		
		public function clear():void
		{
			value = undefined;
		}
		
		public function get autoUpdateFlag( ):AutoUpdateFlag
		{
			return null;
		}
		
		public function contains( e:* ):Boolean
		{
			if ( isEmpty )
				return false;
				
			return value === e;
		}
		
		public function remove( e:* ):void
		{
			if ( contains( e ) )
				value = undefined;
			
		}
		
		public function getMultiplicity(e:*):uint
		{
			if ( contains( e ) )
				return 1;
			return 0;
		}
		
		public function get size():uint
		{
			if ( isEmpty )
				return 0;
			return 1;
		}
		
		public function walk( walker:Function ):void
		{
			if ( isEmpty )
				return;
				
			walker.apply( null, [ value, 1 ] );
		
		}
		
		
		public function any( emptyValue:* = undefined ):*
		{
			if ( isEmpty )
				return emptyValue;
				
			return value;
		}

		
		
//		public function toArray( ):Array
//		{
//			if ( isEmpty )
//				return [];
//			return [ value ];		
//		}
		
		
		public function get asArray( ):Array
		{
			if ( isEmpty )
				return [];
			return [ value ];		
		}
		
		
		
		public function compare( other:* ):ComparisonResult
		{
		
			return ContainerUtil.doCompare( this, other );
		
		}
		
		
		
		
	}
}